
By : Nanda Kishor K N                                      Mail ID : nandakishorkn@rediffmail.com
_________________________________________________________________________________________________

                                       C For Swimmers
                                       ______________

      If u don't know how to swim, learn it now. Otherwise participate in the competition.

_________________________________________________________________________________________________

                               Topic :     Playing with 'printf()' statement
                               _____   _____________________________________

*************************************************************************************************
* NOTE : All the programs are tested under Turbo C/C++ compilers.  				*
* It is assumed that,										*
*			*=> Programs run under DOS environment,					*
*			*=> The underlying machine is an x86 system,				*
*			*=> Necessary header files are included.				*
*			*=> Program is compiled using Turbo C/C++ compiler.			*
* The program output may depend on the information based on this assumptions. 			*
* (For example sizeof(int) = 2 bytes may be assumed). 						*
*************************************************************************************************


[Q001]. What will be the output of the following program :
	void main()
      	{
           printf();
      	}
(a)Run-Time Error	(b)Compile-Time Error		(c)No Output		(d)None of these
Ans. (b) Since there must be enough arguments for the format.
_________________________________________________________________________________________________
	
[Q002]. What will be the output  of the following program :
      	void main()
      	{
          printf(NULL);
      	}
(a)Run-Time Error	(b)Compile-Time Error		(c)No Output		(d)None of these
Ans. (c) Since NULL is a constant value or NULL pointer value or a NULL string.
_________________________________________________________________________________________________

[Q003]. What will be the output of the following program :
 	void main()
      	{
          printf("%%",7);
      	}
(a)7			(b)Compile-Time Error		(c)%			(d)%%
Ans. (c) Since % is a format specifier & excess arguments (more than required by the format) are
merely ignored.
_________________________________________________________________________________________________

[Q004]. What will be the output of the following program :
      	void main()
        {
          printf("//",5);
      	}
(a)5			(b)Compile-Time Error		(c)/			(d)//
Ans. (d) Since / is an escape sequence character & excess arguments (more than required by the format) are merely ignored.
_________________________________________________________________________________________________

[Q005]. What will be the output of the following program :
      	void main()
      	{
          printf("d%",8);
      	}
(a)8			(b)Compile-Time Error		(c)d%			(d)None of these
Ans. (c) Since excess arguments (more than required by the format) are merely ignored.
_________________________________________________________________________________________________

[Q006]. What will be the output of the following program :
      	void main()
      	{
          printf("%d"+0,123);
      	}
(a)123			(b)Compile-Time Error		(c)No Output		(d)None of these
Ans. (a) "%d"+0 has no effect on the output operation. 
_________________________________________________________________________________________________

[Q007]. What will be the output of the following program :
      	void main()
      	{
          printf("%d"+1,123);
      	}
(a)123			(b)Compile-Time Error		(c)d			(d)No Output
Ans. (c) "%d"+1 (i.e 1 or > 0) affects the program output by considering "%d" as string and ignores 123
Where 1 refers to the index i.e. 2nd character in the array or string "%d".
_________________________________________________________________________________________________

[Q008]. What will be the output of the following program :
      	void main()
      	{
          printf("%d",printf("Hi!")+printf("Bye"));
      	}
(a)ByeHi!6		(b)Hi!Bye6		(c)Compile-Time Error		(d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3+3=6
_________________________________________________________________________________________________

[Q009]. What will be the output of the following program :
      	void main()
      	{
          printf("%d",printf("Hi!")*printf("Bye"));
      	}
(a)ByeHi!6		(b)Hi!Bye9		(c)Hi!Bye			(d)None of these
Ans. (b) Since L->R priority & the length of the strings 'Hi!' & 'Bye' is 3*3=9
_________________________________________________________________________________________________

[Q010]. What will be the output of the following program :
      	void main()
      	{
          printf("%d",printf("")+printf(""));
      	}
(a)0			(b)No Output		(c)Compile-Time Error		(d)None of these
Ans. (a) Since L->R priority & the length of the 2 empty strings are : 0+0=0
_________________________________________________________________________________________________

[Q011]. What will be the output of the following program :
      	void main()
      	{
          printf("Hi Friends"+3);
      	}
(a)Hi Friends		(b)Friends		(c)Hi Friends3			(d)None of these
Ans. (b) Since (base adress)+0 points to the value 'H'. Now the NEW (base address) equals
(base address)+3 that points to the character 'F'. Thus it prints the string from 'F' onwards.
_________________________________________________________________________________________________

[Q012]. What will be the output of the following program :
      	void main()
      	{
          printf("C For ") + printf("Swimmers");
      	}
(a)Compile-Time Error	(b)C For Swimmers	(c)Run-Time Error		(d)None of these
Ans. (b) It is a VALID C statement. Change the operators but the output remains same(NO EFFECT).
_________________________________________________________________________________________________

[Q013]. What will be the output of the following program :
      	void main()
      	{
          printf("\/\*\-*\/");
      	}
(a)Run-Time Error	(b)\/*-*\/		(c)/*-*/			(d)None of these
Ans. (c) Since \ is an escape sequence character. Be careful while analyzing such statements.
_________________________________________________________________________________________________

[Q014]. What will be the output of the following program :
      	int main()
      	{
	  int main=7;
	  {
            printf("%d",main);
	    return main;
	  }
          printf("Bye");
      	}
(a)Compile-Time Error	(b)Run-Time Error	(c)7Bye				(d)7
Ans. (d) It is a VALID C statement. Prints 7 and returns the same to the OS. NOTE: Last printf
statement will not be executed.
_________________________________________________________________________________________________

[Q015]. What will be the output of the following program :
      	void main()
      	{
          main();
      	}
(a)Compile-Time Error	(b)Run-Time Error	(c)Infinite Loop		(d)None of these
Ans. (c) It is a VALID C statement. It is like a recursive function & the statements get executed
infinite number of times.
_________________________________________________________________________________________________

[Q016]. What will be the output of the following program :
      	void main()
      	{
          printf("Work" "Hard");
      	}
(a)Work			(b)Hard			(c)No Output			(d)WorkHard
Ans. (d) Since L->R priority. First it prints the word 'Work' & then 'Hard'.
_________________________________________________________________________________________________

[Q017]. What will be the output of the following program :
      	void main()
      	{
          char str[]="%d";
          int val=25;
	  printf(str,val);
      	}
(a)Compile-Time Error	(b)Run-Time Error	(c)25				(d)None of these
Ans. (c) It is a VALID C statement. First parameter contains the format specifier & the Second
parameter contains the actual value 25.
_________________________________________________________________________________________________

[Q018]. What will be the output of the following program :
      	void main()
      	{
          int val=75;
	  printf("%d",val,.,.);
      	}
(a)Compile-Time Error	(b)Unpredictable	(c)75				(d)None of these
Ans. (b) Output is Unpredictable B'coz there are not enough arguments for the format. But it is a
VALID C statement.
_________________________________________________________________________________________________

[Q019]. What will be the output of the following program :
      	void main()
      	{
          int val=10;
	  printf("%d",val+1,"%d",val--);
      	}
(a)10			(b)11 10		(c)11 9				(d)10 9
Ans. (a) Since R->L priority. The second format specifier '%d' is an excess argument and it is
ignored.
_________________________________________________________________________________________________

[Q020]. What will be the output of the following program :
      	void main()
      	{
          int val=5;
	  printf("%d %d %d %d",val,--val,++val,val--);
      	}
(a)3 4 6 5		(b)5 5 6 5 		(c)4 4 5 5			(d)None of these
Ans. (c) Since R->L priority.
_________________________________________________________________________________________________

[Q021]. What will be the output of the following program :
      	void main()
      	{
          int val=5,num;
	  printf("%d",scanf("%d %d",&val,&num));
      	}
[NOTE : ASSUME 2 values are entered by the user are stored in the variables 'val' & 'num' respectively.]
(a)1			(b)2 			(c)5				(d)None of these
Ans. (b) Since scanf statement returns the number of input fields successfully scanned, converted
& stored.
_________________________________________________________________________________________________

[Q022]. What will be the output of the following program :
	#define Compute(x,y,z) (x+y-z)
      	void main()
      	{
          int x=2,y=3,z=4;
	  printf("%d",Compute(y,z,(-x+y)) * Compute(z,x,(-y+z)));
      	}
(a)40			(b)30 			(c)Compile-Time Error		(d)None of these
Ans. (b) Since it is macro function. NOTE : Be careful while doing such type of calculations.
_________________________________________________________________________________________________

[Q023]. What will be the output of the following program :
      	void main()
      	{
          int m=10,n=20;
	  printf("%d %d %d",m/* m-value */,/* n-value */n,m*/* Compute m*n */n);
      	}
(a)Run-Time Error	(b)10 20 200 		(c)Compile-Time Error		(d)None of these
Ans. (b) Since comments /*...*/ are ignored by the compiler.
_________________________________________________________________________________________________

[Q024]. What will be the output of the following program :
      	void main()
      	{
          int m=10,n=20;
	  /* printf("%d",m*n);
      	}
(a)VALID but No Output	(b)VALID : Prints 200	(c)Compile-Time Error		(d)None of these
Ans. (c) Since COMMENT statement not ended properly i.e */ is missing in the above program.
_________________________________________________________________________________________________

[Q025]. What will be the output of the following program :
      	void main()
      	{
          int val=97;
	  "Printing..."+printf("%c",val);
      	}
(a)Printing...97	(b)97			(c)Compile-Time Error		(d)a 
Ans. (d) Since alphabet 'a' is the ASCII equivalent of 97.
_________________________________________________________________________________________________

[Q026]. What will be the output of the following program :
      	void main()
      	{
          int val=5;
	  val=printf("C") + printf("Skills");
          printf("%d",val);
      	}
(a)Skills5		(b)C1			(c)Compile-Time Error		(d)CSkills7 
Ans. (d) VALID Since 'printf' function return the no. of bytes output.
_________________________________________________________________________________________________

[Q027]. What will be the output of the following program :
      	void main()
      	{
          char str[]="Test";
	  if ((printf("%s",str)) == 4) 
          	printf("Success");
	  else
		printf("Failure");
      	}
(a)TestFailure		(b)TestSuccess		(c)Compile-Time Error		(d)Test 
Ans. (b) VALID Since 'printf' function return the no. of bytes output.
_________________________________________________________________________________________________

[Q028]. What will be the output of the following program :
      	void main()
      	{
          int val=5;
 	  printf("%*d",val);
      	}
(a)     5		(b)5			(c)Compile-Time Error		(d)None of these 
Ans. (a) VALID Since '*' specifies the precision (i.e. the next argument in the precision). If no
precision is specified then the value itself will be the precision value. Thus it prints 5 BLANK
SPACES & then the value 5.
_________________________________________________________________________________________________

[Q029]. What will be the output of the following program :
      	void main()
      	{
          int val=5;
 	  printf("%d5",val);
      	}
(a)Compile-Time Error	(b)5			(c)55				(d)     5 
Ans. (c) 
_________________________________________________________________________________________________

[Q030]. What will be the output of the following program :
      	void main()
      	}
          int val=5;
 	  printf("%d",5+val++);
      	{
(a)Compile-Time Error	(b)5			(c)10				(d)11
Ans. (a) Since incorrect usage of pair of braces } and {. Correct usage : Each compound statement
should be enclosed within a pair of braces, i.e { and }.
_________________________________________________________________________________________________
					
				Thanx for using "C For Swimmers".
Regarding this material, you can send Bug Reports, Suggestions, Comments,etc. to nandakishorkn@rediffmail.com